$(function() {
	var n = new Date().getMonth()+1, m = new Date().getDate();
	var currentDate =  n +  "月"+ m + "日";
	$(".current-date").html(currentDate);
	
	var html ="";
    utilModule.get({
    	url: "userCenter/getIndexIM?id="+USERID,
        needJson: false, 
        async: false,
        success: function (response) {
        	console.log(response);
        	if(typeof(response.result) =="undefined"){
        		response.result = response;
        	}
        	if(response.result.length>0){
		      $.each(response.result, function(index, obj) {
		      	if(obj.orderType!=3){
		      		obj.age =","+obj.age+"岁";
		      		obj.url = './html/inquiry/inquiry-service.html?usernameIM='+DOCTOR_IM_PREFFIX+obj.doctorid+'&orderid='+obj.id+'&payStatus='+obj.orderType+'&doctorid='+obj.doctorid;
		      	}else{
		      		obj.age ="";
		      		obj.url = './html/inquiry/inquiry-poor-service.html?usernameIM='+DOCTOR_IM_PREFFIX+obj.doctorid+'&doctorid='+obj.doctorid;
		      		obj.questionbody = "免费";
		      		obj.replyTime = "我关注的医生";
		      	}
		      	obj.message="message";
		      	
		      	if(typeof(obj.deptName) =="undefined"){
		      		obj.deptName = "";
		      	}
		      	if(typeof(obj.realname) =="undefined"){
		      		obj.realname = "等待领取";
		      	}
		      	if(response.orderType==4){
		      		obj.doctortitle = "精防医生";
		      	}else{
		      		if(typeof(obj.doctortitle) =="undefined"){
		      			obj.doctortitle = "";
		      		}
		      	}
		      	
		      	
		        html+=
		            '<li class="font3 '+obj.message+'">'+
		                 '<a class="picture" data-id="'+obj.usernameIM+'" data-href="'+obj.url+'"><img src="'+obj.docAvatar+'" alt=""></a>'
						 + obj.realname+ "  " + obj.doctortitle+
			             '<p class="room font1">'+obj.createtime+'<span class="stand">|</span>'+obj.deptName+'</p>'+
			             '<p class="write font2 mui-ellipsis-2">'+obj.questionbody+
			             '<font>('+obj.sex+obj.age+')</font></p>'+
			             '<a  data-href="'+obj.url+'" class="color_blue font1">'+obj.replyTime+'</a>'+
		            '</li>'
		      });
		    }else{
		    	$(".noMessage").show();
		    }
        	    
		   $("#content_oj").append(html);
        }
    });
    
	var getIndexStorage = (function() {
    	var index_doctor = utilModule.getStorage("index_doctor");
    	if(index_doctor != "null" && index_doctor != "undefined" && index_doctor != ''){
			return JSON.parse(index_doctor);
    	}else{
    		return false;
    	}
    	
    });
	// 取 缓存
	/*
    var doctorlist = getIndexStorage();
    var html ="";
    var type1 ="";
    var type2 ="";
    if(doctorlist){
      $.each(doctorlist.doctorlist, function(index, obj) {
      	if(obj.payStatus ==1){
      		type1+=obj.orderid+",";
      	}else{
      		type2+=obj.orderid+",";
      	}
        html+=
            '<li class="font3 message">'+
                 '<a class="picture" href="../../html/inquiry/inquiry-service.html?usernameIM='+obj.usernameIM+'&orderid='+obj.orderid+'&payStatus='+obj.payStatus+'"><img src="'+obj.avatar+'" alt=""></a>'
				 + obj.realname+
	             '<p class="room font1">'+obj.time+'<span class="stand">|</span>'+obj.deptName+'</p>'+
	             '<p class="write font2 mui-ellipsis-2">'+obj.msg+'....<font>'+obj.sex_old+'</font></p>'+
	             '<a href="../../html/inquiry/inquiry-service.html?usernameIM='+obj.usernameIM+'&orderid='+obj.orderid+'&payStatus='+obj.payStatus+'" class="color_blue font1">'+obj.status+'</a>'+
            '</li>'
      });
      	if(type1 ==""){
      		type1=0
      	}
      	if(type2 ==""){
      		type2=0
      	}
     	// var type1 ="1166,882,881,";
    	// var type2 ="1166,2460,3";
      	utilModule.get({
        url: "quickquestion/queryFinishOrderList?type1="+type1+"&type2="+type2,
        // data: {"id":orderid},
        needJson: false, 
        async: false,
        success: function (response) {
        	if(response.length>0){
        		var a= "";
        		for (var i = response.length - 1; i >= 0; i--) {
        			$.each(doctorlist.doctorlist, function(index, obj) {
        				if(response[i].id ==obj.orderid &&response[i].isCommented ==obj.payStatus){
        					//delete doctorlist.doctorlist[i];
							a+=i+"," 
							if(chatList !=null){
			                	var chatObject = JSON.parse(chatList);
			                	var rd_key = DOCTOR_IM_PREFFIX+ response[i].doctorid;
			                	delete chatObject[rd_key];

				                var $chatObject = JSON.stringify(chatObject);
				                if($chatObject.length ==2){
				                	utilModule.removeStorage("chat_list");
				                }else{
				                	utilModule.setStorage("chat_list", $chatObject);
				                }
				                
			            	}
        				}
        			});
        		}
        		if(a != ""){
	        		var b =a.slice(0,-1).split(",");
	        		for (var i = b.length - 1; i >= 0; i--) {
	        			var a =b[i];
	        			doctorlist.doctorlist.splice(a, 1);
	        			//delete doctorlist.doctorlist[a];
	        		}
	        		utilModule.removeStorage("index_doctor");
	        		if(JSON.stringify(doctorlist).length!=17){
	        			utilModule.setStorage("index_doctor",JSON.stringify(doctorlist));	
	        		}
	        		window.location.reload()

	        		
        		}
        		
        	}
        }
    	});
    }
    $("#content_oj").append(html);*/
	$(".index-contentBox .message").on('click',function(){
		 var id = $(this).find("a").data("id");
 		if(id == "undefined"){
 			mui.alert("请等待医生抢单");
 		}else{
   			location.href = "../../"+$(this).find("a").attr("data-href");
   		}
	});
	
	$(".html-loding").hide();
	
	document.getElementById('index').addEventListener('tap',function(){
		mui.openWindow({
			url:'../../index.html',
    		id:'index',
		});
	});
	document.getElementById('message').addEventListener('tap',function(){
		mui.openWindow({
			url:'../../html/inquiry/message-list.html',
    		id:'message',
		});
	});
	document.getElementById('article').addEventListener('tap',function(){
		mui.openWindow({
			url:'../../html/article/article-list.html',
    		id:'article',
		});
	});
	document.getElementById('userCenter').addEventListener('tap',function(){
		mui.openWindow({
			url:'../../html/userCenter/user-center.html',
    		id:'userCenter',
		});
	});
});

